<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 百钱
 */
class BaiqianController extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));

        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $isMobile = isMobile();

        $return = $this->orderadd( $parameter );

        $data = array();
        $data['X1_Amount'] = number_format($return['amount'],2,'.','');
        $data['X2_BillNo'] = $return['orderid'];
        $data['X3_MerNo'] = $this->config['mch_id'];
        $data['X4_ReturnURL'] = $return['notifyurl'];
        ksort($data);
        $str = httpstr($data)."&". strtoupper(md5($this->config['signkey']));

        $data['X6_MD5info'] =strtoupper(md5($str));
        $data['X5_NotifyURL'] = $return['callbackurl'];

        switch($this->config['paytype']){
            //微信
            case 1:
                $data['X7_PaymentType'] = "WXSM";
                break;
            //微信H5
            case 2:
                $data['X7_PaymentType'] = "WXH5";
                break;
            //支付宝
            case 3:
            case 17:
            case 18:
                if($isMobile){
                    $data['X7_PaymentType'] = "ALIPAYSM";
                }else{
                    $data['X7_PaymentType'] = "ALIPAYSM";
                }
                //$data['X7_PaymentType'] = "ALIPAYSM";
                break;
            //支付宝H5
            case 4:
                //$data['X7_PaymentType'] = "ALIPAYH5";
                if($isMobile){
                    $data['X7_PaymentType'] = "ALIPAYSM";
                }else{
                    $data['X7_PaymentType'] = "ALIPAYSM";
                }
                break;
            //银联扫码
            case 11:
                $data['X7_PaymentType'] = "BSM";
                break;
            //银联快捷
            case 13:
                $data['X7_PaymentType'] = "KJZF";
                break;
            //网银直连
            case 6:
                switch($product['code']) {
                    //北京银行
                    case 'BOB':
                        $data['X7_PaymentType'] = 'BCCB';
                        break;
                    //东亚银行
                    case 'BEA':
                        $data['X7_PaymentType'] = 'HKBEA';
                        break;
                    //中国工商银行
                    case 'ICBC':
                        $data['X7_PaymentType'] = 'ICBC';
                        break;
                    //光大银行
                    case 'CEB':
                        $data['X7_PaymentType'] = 'CEB';
                        break;
                    //广发银行
                    case 'CGB':
                        $data['X7_PaymentType'] = 'GDB';
                        break;
                    //华夏银行
                    case 'HXB':
                        $data['X7_PaymentType'] = 'HXB';
                        break;
                    //中国建设银行
                    case 'CCB':
                        $data['X7_PaymentType'] = 'CCB';
                        break;
                    //交通银行
                    case 'BOCOM':
                        $data['X7_PaymentType'] = 'BOCO';
                        break;
                    //中国民生银行
                    case 'CMBC':
                        $data['X7_PaymentType'] = 'CMBC';
                        break;
                    //南京银行
                    case 'NJCB':
                        $data['X7_PaymentType'] = 'NJCB';
                        break;
                    //宁波银行
                    case 'NBCB':
                        $this->showmessage('暂不支持此银行卡!', ['code'=>C("THIRDCODES.".$this->class)]);
                        break;
                    //中国农业银行
                    case 'ABC':
                        $data['X7_PaymentType'] = 'ABC';
                        break;
                    //平安银行
                    case 'PAB':
                        $data['X7_PaymentType'] = 'PINGANBANK';
                        break;
                    //上海银行
                    case 'SHB':
                        $data['X7_PaymentType'] = 'SHB';
                        break;
                    //上海浦东发展银行
                    case 'SPDB':
                        $data['X7_PaymentType'] = 'SPDB';
                        break;
                    //深圳发展银行
                    case 'SDB':
                        $data['X7_PaymentType'] = 'SDB';
                        break;
                    //兴业银行
                    case 'CIB':
                        $data['X7_PaymentType'] = 'CIB';
                        break;
                    //中国邮政储蓄银行
                    case 'PSBS':
                        $data['X7_PaymentType'] = 'POST';
                        break;
                    //招商银行
                    case 'CMB':
                        $data['X7_PaymentType'] = 'CMBCHINA';
                        break;
                    //浙商银行
                    case 'CZB':
                        $data['X7_PaymentType'] = 'CZ';
                        break;
                    //中国银行
                    case 'BOC':
                        $data['X7_PaymentType'] = 'BOC';
                        break;
                    //中信银行
                    case 'CTTIC':
                        $data['X7_PaymentType'] = 'ECITIC';
                        break;
                    default:
                        $this->showmessage('暂不支持此银行卡!', ['code'=>C("THIRDCODES.".$this->class)]);
                }
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }
        $data['X8_MerRemark'] = 'zf';
        $data['X9_ClientIp'] = $_SERVER['REMOTE_ADDR'];

        if($this->config['paytype'] == 6){
            //网银
            po($this->config['gateway'] , $data);
        }

        $res = curl($this->config['gateway'] , httpstr($data));

        $res = json_decode($res, true);
        if(!$res || empty($res)){
            $this->showmessage('获取数据失败!',['code'=>C("THIRDCODES.".$this->class)]);
        }
		
		//echo '<pre/>'; print_r($data); print_r($res); exit;
		

        $code = $res['imgUrl'];
        if(!$code){
            if($res['msg']){
                $this->showmessage($res['msg'],['code'=>C("THIRDCODES.".$this->class)]);
            }
            $this->showmessage('获取验证码失败!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        //返回json
        if($_REQUEST['pay_returntype']){
            $this->showJson("", $code);
        }

        //echo '<pre/>'; print_r($res); exit;
        switch($this->config['paytype']){
            //支付宝
            //支付宝H5
            case 3:
            case 4:
                if(isMobile()){
                    //header("Location: {$code}"); exit;
                }
                break;
            //微信H5
            case 2:
                //银联快捷
            case 13:
                header("Location: {$code}"); exit;
            default:

        }

        $paytype = transArr( C("PAYTYPES"), "id");

        $this->assign("wap_url", $code);
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?data=".urlencode($code));
        //$this->display("WeiXin/Pay");

        switch($this->config['paytype']){
            //微信
            case 1:
                $this->display("WeiXin/weixin");
                break;
            //支付宝扫码
            case 3:
                //支付宝H5
            case 4:
            case 17:
            case 18:
                if($isMobile){
                    $this->display("WeiXin/alipaywap");
                }else{
                    $this->display("WeiXin/alipay");
                }
                break;
            default:
                $this->display("WeiXin/Pay");
        }
    }

    //通知
    public function notifyurl(){
        $p = $origin = file_get_contents('php://input');
        //$p = '{"MerNo":"1557597679","Amount":"10.00","BillNo":"201810182049114585121","Succeed":88,"MD5info":"01D1928F081485C5D40211376262DE5B","Result":"\u652f\u4ed8\u6210\u529f","MerRemark":"zf"}';

        $p = json_decode($p, true);

        /*
         * Array
        (
            [MerNo] => 1557597679
            [Amount] => 10.00
            [BillNo] => 201810182049114585121
            [Succeed] => 88
            [MD5info] => 01D1928F081485C5D40211376262DE5B
            [Result] => 支付成功
            [MerRemark] => zf
        )
         */

        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['BillNo'];
        $find = M("Order")->where(array("pay_orderid"=>$orderid))->find();
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "server_return"=> $origin,
            //"pay_orderid_rd"=>$p['sysnumber']
        ));

        /*Order::getInstance()->edit($find['id'],array(
            "server_return"=> $origin,
            //"pay_orderid_rd"=>$p['sysnumber']
        ));
        Ordermember::getInstance(getIdByBianHao($find['pay_memberid']))->edit($find['id'],array(
            "server_return"=> $origin,
            //"pay_orderid_rd"=>$p['sysnumber']
        ));*/

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>'Baiqian'))->find();

        $sign = $p['MD5info'];
        $data = [];
        $data['MerNo'] = $p['MerNo'];
        $data['Amount'] = $p['Amount'];
        $data['BillNo'] = $p['BillNo'];
        $data['Succeed'] = $p['Succeed'];

        ksort($data);
        $str = httpstr($data)."&". strtoupper(md5($this->config['signkey']));

        $sign2 =strtoupper(md5($str));

        //echo 'sign: '.$sign.'<br/>';
        //echo 'sign2:'.$sign2;
        //exit;
        if($sign != $sign2){
            exit("sign_wrong");
        }

        if($p['Succeed'] != 88){
            exit("pay_fail");
        }

        //处理支付
        //$this->EditMoney($find['pay_orderid'],"Baiqian",0);

        //回调成功
        //echo 'SUCCESS'; exit;
        $this->async($find['pay_orderid'],'SUCCESS');
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}